/*
 * Copyright 2008-2023 dexian.vip. All rights reserved.
 * Support: http://www.dexian.vip
 * License: http://www.dexian.vip/license
 */

package vip.dexian.core.base.bean;

import jakarta.persistence.Column;
import jakarta.persistence.MappedSuperclass;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.experimental.SuperBuilder;
import org.apache.commons.lang3.builder.CompareToBuilder;
import org.hibernate.annotations.Comment;

import java.io.Serial;
import java.io.Serializable;

/**
 * 有唯一索引，但是又需要排序属性的实体类
 *
 * @author 挺好的 2023年06月02日 8:28
 */
@MappedSuperclass
@Data
@NoArgsConstructor
@AllArgsConstructor
@SuperBuilder
public abstract class UniqueOrderEntity<ID extends Serializable> extends BaseEntity <ID>
        implements Comparable <UniqueOrderEntity <ID>> {

    @Serial
    private static final long serialVersionUID = 6901288282194210164L;

    /**
     * 排序
     */
    @Comment ("排序")
    @Column (nullable = false, name = "orders")
    private Integer order;

    /**
     * 唯一索引复核标记，使用当前系统的时间戳
     */
    @Comment ("唯一索引标记")
    @Column (nullable = false)
    private Long uniqueTimestamp;

    /**
     * @param o
     *         the object to be compared.
     *
     * @return a negative integer, zero, or a positive integer as this object is less than, equal to, or greater than the specified object.
     *
     * @see Comparable
     */
    @Override
    public int compareTo (UniqueOrderEntity <ID> o) {
        if (o == null) {
            return 1;
        }
        return new CompareToBuilder().append(this.getOrder(), o.getOrder()).append(this.getId(), o.getId())
                .toComparison();
    }
}
